﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AlgoTester
{
    class Date : IComparable<Date>
    {
        // Constants
        private static int[] daysInMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        private static int monthsInYear = 12;

        // Data Members
        int day;
        int month;
        int year;

        // Constructor
        public Date(int day, int month, int year)
        {
            this.day = day;
            this.month = month;
            this.year = year;
        }

        public int getDay()
        {
            return day;
        }

        public int getMonth()
        {
            return month;
        }

        public int getYear()
        {
            return year;
        }

        // Function to increment to next Day
        public void increment()
        {
            if (!endOfMonth(day))
                ++day;
            else
                if (month < 12)
                {
                    ++month;
                    day = 1;
                }
                else
                {
                    ++year;
                    month = day = 1;
                }
        }

        // Function to decrement to the previous Day
        public void decrement()
        {
            if (day > 1)
                --day;
            else
                if (month > 1)
                {
                    --month;
                    day = daysInMonth[month];
                }
                else
                {
                    --year;
                    month = monthsInYear;
                    day = daysInMonth[monthsInYear];
                }
        }

        // Comparison method
        public int CompareTo(Date otherDate)
        {
            int yearDiff = year - otherDate.getYear();
            if (yearDiff != 0)
                return yearDiff;
            int monthDiff = month - otherDate.getMonth();
            if (monthDiff != 0)
                return monthDiff;
            return day - otherDate.getDay();
        }

        // Tests if arg is the day at the end of a Month
        private bool endOfMonth(int testDay)
        {
            if (month == 2 && isLeapYear(year))
                return testDay == 29;
            else
                return testDay == daysInMonth[month];
        }

        // Tests if given year is a leap year
        private bool isLeapYear(int testYear)
        {
            return (testYear % 400 == 0 || (testYear % 100 != 0 && testYear % 4 == 0));
        }

    }
}
